package net.entelijan.cobean.util;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

/**
 * Testcases for the PropertyComparator
 *
 */
public class PropertyComparatorTestCase {

	@Test
	public void test() {
		List<SortBean> beans = createSortBeans();
		PropertyComparator<SortBean> ps = new PropertyComparator<SortBean>();
		ps.setPropertyName("intVal");
		ps.setAscending(false);
		Collections.sort(beans, ps);
		assertEquals(null, beans.get(0).getStrVal());
		assertEquals("11_STRING", beans.get(1).getStrVal());
		assertEquals("10_STRING", beans.get(2).getStrVal());
		assertEquals("9_STRING", beans.get(3).getStrVal());
		assertEquals("8_STRING", beans.get(4).getStrVal());
		assertEquals("7_STRING", beans.get(5).getStrVal());
		assertEquals("6_STRING", beans.get(6).getStrVal());
		assertEquals("5_STRING", beans.get(7).getStrVal());
		assertEquals("4_STRING", beans.get(8).getStrVal());
		assertEquals("3_STRING", beans.get(9).getStrVal());
		assertEquals("2_STRING", beans.get(10).getStrVal());
		assertEquals("1_STRING", beans.get(11).getStrVal());
	}

	@Test
	public void test1() {
		List<SortBean> beans = createSortBeans();
		PropertyComparator<SortBean> ps = new PropertyComparator<SortBean>();
		ps.setPropertyName("intVal");
		ps.setAscending(true);
		Collections.sort(beans, ps);
		assertEquals(null, beans.get(11).getStrVal());
		assertEquals("11_STRING", beans.get(10).getStrVal());
		assertEquals("10_STRING", beans.get(9).getStrVal());
		assertEquals("9_STRING", beans.get(8).getStrVal());
		assertEquals("8_STRING", beans.get(7).getStrVal());
		assertEquals("7_STRING", beans.get(6).getStrVal());
		assertEquals("6_STRING", beans.get(5).getStrVal());
		assertEquals("5_STRING", beans.get(4).getStrVal());
		assertEquals("4_STRING", beans.get(3).getStrVal());
		assertEquals("3_STRING", beans.get(2).getStrVal());
		assertEquals("2_STRING", beans.get(1).getStrVal());
		assertEquals("1_STRING", beans.get(0).getStrVal());
	}

	@Test
	public void test2() {
		List<SortBean> beans = createSortBeans();
		PropertyComparator<SortBean> ps = new PropertyComparator<SortBean>();
		ps.setPropertyName("strVal");
		ps.setAscending(false);
		Collections.sort(beans, ps);
		assertEquals(null, beans.get(0).getStrVal());
		assertEquals("9_STRING", beans.get(1).getStrVal());
		assertEquals("8_STRING", beans.get(2).getStrVal());
		assertEquals("7_STRING", beans.get(3).getStrVal());
		assertEquals("6_STRING", beans.get(4).getStrVal());
		assertEquals("5_STRING", beans.get(5).getStrVal());
		assertEquals("4_STRING", beans.get(6).getStrVal());
		assertEquals("3_STRING", beans.get(7).getStrVal());
		assertEquals("2_STRING", beans.get(8).getStrVal());
		assertEquals("1_STRING", beans.get(9).getStrVal());
		assertEquals("11_STRING", beans.get(10).getStrVal());
		assertEquals("10_STRING", beans.get(11).getStrVal());
	}

	@Test
	public void test3() {
		List<SortBean> beans = createSortBeans();
		PropertyComparator<SortBean> ps = new PropertyComparator<SortBean>();
		ps.setPropertyName("strVal");
		ps.setAscending(true);
		Collections.sort(beans, ps);
		assertEquals(null, beans.get(11).getStrVal());
		assertEquals("9_STRING", beans.get(10).getStrVal());
		assertEquals("8_STRING", beans.get(9).getStrVal());
		assertEquals("7_STRING", beans.get(8).getStrVal());
		assertEquals("6_STRING", beans.get(7).getStrVal());
		assertEquals("5_STRING", beans.get(6).getStrVal());
		assertEquals("4_STRING", beans.get(5).getStrVal());
		assertEquals("3_STRING", beans.get(4).getStrVal());
		assertEquals("2_STRING", beans.get(3).getStrVal());
		assertEquals("1_STRING", beans.get(2).getStrVal());
		assertEquals("11_STRING", beans.get(1).getStrVal());
		assertEquals("10_STRING", beans.get(0).getStrVal());
	}

	private List<SortBean> createSortBeans() {
		ArrayList<SortBean> re = new ArrayList<SortBean>();
		for (int i = 1; i <= 11; i++) {
			re.add(new SortBean(i, i + "_STRING"));
		}
		re.add(new SortBean(12, null));
		return re;
	}

}
